package com.game.raceroom.player.disp
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import com.util.DEF;
	import com.game.raceroom.map.Map001_bg;
	import com.game.raceroom.map.Map001_fg;
	import com.game.raceroom.map.Map002_bg;
	import com.game.raceroom.map.Map002_fg;
	import com.game.raceroom.map.Map003_bg;
	import com.game.raceroom.map.Map003_fg;
	import com.game.raceroom.player.MapInfo;
	import com.game.raceroom.player.contrl.MapContrl;
	
	import mx.containers.Canvas;
	import mx.core.UIComponent;
	
	public class MapDisplayer 
	{
        public var mapImage:Bitmap;
        private var mapData:BitmapData=new BitmapData(DEF.GAME_CANVAS_WIDTH,DEF.GAME_CANVAS_HEIGHT);
        private var catchData:BitmapData=new BitmapData(DEF.GAME_CANVAS_WIDTH,DEF.GAME_CANVAS_HEIGHT);
        private var map_width,map_height,map_x,map_y:int;
        private var mapContrl:MapContrl;
        private var comp:UIComponent= new UIComponent();
        private var mapInfo:MapInfo;
        
        private var bg:UIComponent;
        private var fg:UIComponent;

		public function MapDisplayer(bg:UIComponent,fg:UIComponent,mapCtrl:MapContrl):void
		{
			 
			this.mapContrl=mapCtrl;
			this.bg=bg;
			this.fg=fg;

		}
		public function drawMap():void{

			
 
		}

        // var start_x,
        public var screen_start_x,screen_start_y,screen_end_x,screen_end_y:int;
        public var actor_disp_x,actor_disp_y:int;
        private var rect,rect1:Rectangle;
        private var point:Point=new Point;
        private var point1:Point=new Point(0,0);
        private var x_tile_st,y_tile_st,x_tile_ed,y_tile_ed,x_off,y_off:int;
		private var line,tid,kit_y,kit_x,ii,jj:int;
		public function updateMap(center_x:Number,center_y:Number):void{
            center_x=Math.round(center_x);
            center_y=Math.round(center_y);
			screen_start_x=center_x-DEF.GAME_CANVAS_WIDTH/2;
			screen_start_y=center_y-DEF.GAME_CANVAS_HEIGHT/2;
			screen_end_x=center_x+DEF.GAME_CANVAS_WIDTH/2-1;
			screen_end_y=center_y+DEF.GAME_CANVAS_HEIGHT/2-1;  
   
			if(screen_start_x<0){
				//screen_end_x+=Math.abs(screen_start_x);
				screen_start_x=0;
				
			}else if(screen_end_x>mapInfo.map_width-1){
				screen_start_x=mapInfo.map_width-DEF.GAME_CANVAS_WIDTH;
			 
			}   
			screen_end_x =screen_start_x+DEF.GAME_CANVAS_WIDTH;      
 			if(screen_start_y<0){
				screen_start_y=0;
			}else if(screen_end_y>mapInfo.map_height-1){
				screen_start_y=mapInfo.map_height-DEF.GAME_CANVAS_HEIGHT;
			} 
			screen_end_y =screen_start_y+DEF.GAME_CANVAS_HEIGHT; 
mapbg.x= -screen_start_x;
mapbg.y= -screen_start_y;
mapfg.x=mapbg.x;
mapfg.y=mapbg.y;
//			x_tile_st = Math.floor(screen_start_x / mapInfo.tile_width);
//			y_tile_st = Math.floor(screen_start_y / mapInfo.tile_height);
//			x_tile_ed = Math.round(screen_end_x / mapInfo.tile_width);
//			y_tile_ed = Math.round(screen_end_y / mapInfo.tile_height);
//			x_off=(screen_start_x % mapInfo.tile_width)*-1;
//			y_off=(screen_start_y % mapInfo.tile_height)*-1;

//            for(ii=y_tile_st;ii<=y_tile_ed;ii++){
//            	line=ii*mapInfo.width_in_tiles;
//            	for(jj=x_tile_st;jj<=x_tile_ed;jj++){
//	            	point.x=x_off;
//	            	point.y=y_off;
//	            	tid=mapInfo.viewIdx[line+jj];
//	            	kit_y=Math.floor((tid/kit_width_in_tile))*mapInfo.tile_width;
//	            	kit_x=(tid%kit_width_in_tile)*mapInfo.tile_height;
//	            	rect.x=kit_x;
//	            	rect.y=kit_y;
//	            	
//	                 mapData.copyPixels(mapInfo.tilekit.bitmapData,rect,point);
//	                x_off+=	mapInfo.tile_width;
//            	}
//            	x_off=(screen_start_x % mapInfo.tile_width)*-1
//            	y_off+=mapInfo.tile_height;
//            }

            //this.mapData.copyPixels(catchData,rect1,point1);
 // mapImage.bitmapData=mapData;
           // y_off = - (disp_top % tile_height);
		}
		private var kit_height,kit_width,kit_height_in_tile,kit_width_in_tile:int;
  		var mapbg:Canvas;
		var mapfg:Canvas;
		public function drawFirst(mapInfo:MapInfo):void{
		 
			this.mapInfo=mapInfo;
            switch(mapInfo.mapid){
				case 2:
  		         mapbg=new Map003_bg;
		         mapfg=new Map003_fg;
				break; 
				case 1:
  		         mapbg=new Map002_bg;
		         mapfg=new Map002_fg;
				break;
				case 0:
				default:
  		         mapbg=new Map001_bg;
		         mapfg=new Map001_fg;
				break;
			}
//			mapImage=new Bitmap(mapData);
//			kit_width=mapInfo.tilekit.width;
//			kit_height=mapInfo.tilekit.height;
//			kit_width_in_tile=kit_width/mapInfo.tile_width;
//			kit_height_in_tile=kit_height/mapInfo.tile_height;
			bg.addChild(mapbg);
			fg.addChild(mapfg);
			
//			rect=new Rectangle(0,0, mapInfo.tile_width,mapInfo.tile_height);
//			rect1=new Rectangle(0,0, this.mapData.width,this.mapData.width);
//			point1=new Point(0,0);
			//updateMap(mapInfo.start_x,mapInfo.start_y);
		}
		public function destroy():void{
			bg.removeChild(mapbg);
			fg.removeChild(mapfg);
			mapImage=null;
		}

	}
}